<template>
	<view class="content">
		<button type="default" @click="setstorageSync1">设置内存</button>
		<button type="default" @click="setstorage1">设置内存异步</button>
		<button type="primary" @click="getstorageSync1">获取内存</button>
		<button type="primary" @click="getstorage1">获取内存异步</button>
		<button type="warn" @click="getStorageInfoSync1">获取全部内存</button>
		<button type="warn" @click="getStorageInfo1">获取全部内存异步</button>
		<button type="default" @click="delstorageSync1">移除内存值uuid</button>
		<button type="default" @click="delstorage1">移除内存值age异步</button>
		<button type="primary" @click="removeallstorage">清除全部内存值</button>
	</view>
	<view class="content">
		<text>获取内存:{{uuidSync}}</text>
		<text>获取内存异步:{{uuid}}</text>
	</view>
	
</template>

<script setup>
import {computed, ref} from "vue"

function setstorageSync1(){
	uni.setStorageSync("uuid","1234567");
	uni.setStorageSync("age","18")
}	
	
async function setstorage1(){
	await uni.setStorage({
		key:"uuid",
		data:"123456",
		success:()=>{
			console.log("设置完成");
		}
	})
}

const uuidSync = ref()

function getstorageSync1(){
	uuidSync.value = uni.getStorageSync("uuid")
}

const uuid = ref()

async function getstorage1(){
	await uni.getStorage({
		key:"uuid",
		success: (e) => {
			uuid.value = e.data
		}
	})
}


// #同步获取当前 storage 的相关信息
function getStorageInfoSync1(){
	const res = uni.getStorageInfoSync();
	console.log(res.keys);
	console.log(res.currentSize);
	console.log(res.limitSize);
}

async function getStorageInfo1(){
	const res = await uni.getStorageInfoSync();
	console.log(res.keys);
	console.log(res.currentSize);
	console.log(res.limitSize);
	const listkey = res.keys.map(item=>{
		let keyval={}
		uni.getStorage({
			key:item,
			success: (e) => {
				keyval[item] = e.data
			}
		})
		return keyval
	})
	return listkey
}

function delstorageSync1(){
	uni.removeStorageSync("uuid")
}

function delstorage1(){
	uni.removeStorage({
		key:"age",
		success: () => {
			console.log("移除完成");
		}
	})
}

function removeallstorage(){
	uni.clearStorageSync()
}

uni.setStorage({
	key:"uuid",
	data:"123",
	success:()=>{
		console.log("设置完成");
	}
})
	
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
